<template>
<div>
<el-container>
    <el-header>
      当前用户：<el-link>{{currentUser.username}}</el-link>
      <el-button @click="loginout">登出</el-button>
      <h2>购票列表</h2>
    </el-header>
  <hr>
  <div>
    <el-main>
      始发站：<el-select v-model="searchTrain.begin" placeholder="请选择">
      <el-option value=""></el-option>
        <el-option
          v-for="item in options"
          :key="item.id"
          :label="item.name"
          :value="item.name">
        </el-option>
      </el-select>
      乘车日期<el-input v-model="searchTrain.byTime1" type="date" style="width: 200px" placeholder="请输入内容"></el-input>
      -
      <el-input v-model="searchTrain.byTime2" type="date" style="width: 200px" placeholder="请输入内容"></el-input>
      姓名：<el-input v-model="searchTrain.name" type="text" style="width: 200px" placeholder="请输入姓名"></el-input>
      <el-button @click="query">查询</el-button>
      <el-table
        :data="tableData"
        style="width: 100%">
        <el-table-column
          type="index"
          label="序号">
        </el-table-column>
        <el-table-column
          prop="id"
          label="id">
        </el-table-column>
        <el-table-column
          prop="name"
          label="购票人">
        </el-table-column>
        <el-table-column
          prop="begin"
          label="始发站">
        </el-table-column>
        <el-table-column
          prop="end"
          label="到达站">
        </el-table-column>
        <el-table-column
          prop="buyTime"
          label="购票时间">
        </el-table-column>
        <el-table-column
          prop="byTime"
          label="乘车日期">
        </el-table-column>
        <el-table-column
          prop="price"
          label="票价">
        </el-table-column>
        <el-table-column>
          <template slot-scope="scope">
            <el-button size="mini" type="primary" plain @click="del(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-main>
    <el-footer>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="searchTrain.page"
        :page-size="searchTrain.pageSize"
        layout="total, prev, pager, next"
        :total="searchTrain.total">
      </el-pagination>
    </el-footer>
  </div>
</el-container>
</div>
</template>

<script>
export default {
  name: 'TicketView',
  data () {
    return {
      // 模糊查询字段
      searchTrain: {
        page: 1,
        pageSize: 5,
        key: '',
        total: null,
        id: null,
        name: '',
        beginId: null,
        endId: null,
        buyTime: '',
        byTime1: '',
        byTime2: '',
        price: '',
        begin: '',
        end: ''
      },
      // 列表数据
      tableData: [],
      // 始发站下拉框数据
      options: []
    }
  },
  created () {
    this.list()
    // 页面加载时获取始发站下拉框数据
    this.axios.post('/region/listRegion').then(res => {
      this.options = res.data.jdata
    })
  },
  // 计算字段
  computed: {
    currentUser () {
      return this.$store.state.userInfo
    }
  },
  methods: {
    // 当前页显示条数
    handleSizeChange (val) {
      this.searchTrain.pageSize = val
      this.list()
      console.log(`每页 ${val} 条`)
    },
    // 当前页码
    handleCurrentChange (val) {
      this.searchTrain.page = val
      this.list()
      console.log(`当前页 ${val} `)
    },
    del (id) {
      this.axios.get('/train/del', { params: { id: id } }).then(res => {
        if (res.data.code === 200) {
          this.$message.success('删除成功')
          location.reload()
        } else {
          this.$message.success('删除失败')
        }
      })
    },
    // 条件查询
    query () {
      this.axios.post('/train/listTrainv', this.searchTrain).then(res => {
        this.tableData = res.data.records
        this.searchTrain.page = res.data.current
        this.searchTrain.pageSize = res.data.size
        this.searchTrain.total = res.data.total
      })
    },
    // 登出
    loginout () {
      this.axios.post('/myLoginout').then(res => {
        this.$store.commit('setLoginUser', {})
        this.$router.push('/login')
      })
    },
    // 列表
    list () {
      this.axios.post('/train/listTrainv', this.searchTrain).then(res => {
        this.tableData = res.data.records
        this.searchTrain.page = res.data.current
        this.searchTrain.pageSize = res.data.size
        this.searchTrain.total = res.data.total
      })
    }
  }
}
</script>

<style scoped>

</style>
